package com.vertx.blocking;

import com.vertx.util.Runner;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.WorkerExecutor;

public class WorkerVerticle extends AbstractVerticle {
	public static void main(String[] args) {

		Runner.runExample(WorkerVerticle.class);

	}
	@Override
	public void start(Promise<Void> startPromise) throws Exception {

		System.out.println("begin");

//		vertx.executeBlocking(future -> {
//			// 调用一些需要耗费显著执行时间返回结果的阻塞式API
//			String result = "";
//			try {
//				Thread.sleep(3000);
//				result = "sleep 3000";
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//			future.complete(result);
//		}, res -> {
//			System.out.println("The result is: " + res.result());
//		});



		WorkerExecutor executor = vertx.createSharedWorkerExecutor("my-worker-pool");
		executor.executeBlocking(future -> {
			// 调用一些需要耗费显著执行时间返回结果的阻塞式API
			String result = "";
			try {
				Thread.sleep(3000);
				result = "sleep 3000";
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			future.complete(result);
		}, res -> {
			System.out.println("The result is: " + res.result());
		});

		System.out.println("end");
	}
}
